import java.util.Arrays;

/**
 * @author dx
 * @version 1.0
 * @date 2022/5/18 9:29
 * @description: 力扣 88 合并两个有序数组
 */
public class Merge {
    public static void main(String[] args) {
        int []test1 = {1,2,3,0,0,0};
        int []test2 = {2,5,6};
        new Merge_Solution().merge1(test1,3,test2,2);
    }
}

class Merge_Solution{
    public void merge1(int[] nums1, int m, int[] nums2, int n) {
        int []num = new int[m+n];
        int []num_t = new int[m];
        for(int i = 0;i<m;i++){
            num_t[i] = nums1[i];
        }
        int len = m+n;
        int index = len-1;
        int a = m-1,b = n-1;
        while(a >=0 && b>=0) {
            num[index--] = num_t[a] >= nums2[b] ? num_t[a--] : nums2[b--];
        }
        if(a >= 0){
            System.arraycopy(num_t,0,num,0,a+1);  //tip:数组复制
        }else if(b>=0){
            System.arraycopy(nums2,0,num,0,b+1);
        }
        System.arraycopy(num,0,nums1,0,m+n);
    }
}